using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_usrRelationship : System.Web.UI.UserControl
{
    protected string strRelation = "Select";
    protected string strValidatinoGroup = "";
    protected string strErrorMessage = "Choose relationship";
    protected bool bEnabled = true;
    bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvRelationship.Enabled && rfvRelationship.ValidationGroup == strValidationGroup)
        {
            rfvRelationship.Validate();
            if (rfvRelationship.IsValid == false)
            {
                ddlRelation.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlRelation.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlRelation.ControlStyle.CssClass = "";
        }
    }

    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    // Test whether the value is -1
    //    if (args.Value != "-1")
    //    {
    //        args.IsValid = true;
    //    }
    //    else
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            //SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvRelationship.Enabled = bValue;
    }

    public string Relation
    {
        get { return strRelation; }
        set { strRelation = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlRelation.SelectedIndex = 0;
        ddlRelation.Enabled = bEnabled;
        rfvRelationship.Enabled = bEnabled;
    }

    protected override void OnPreRender(EventArgs e)
    {
        SetEnabled();

        if (strRelation != string.Empty)
        {
            if (strRelation != "Select")
                ddlRelation.Text = strRelation;
        }

        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
                rfvRelationship.ValidationGroup = strValidatinoGroup;
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }

        if (strErrorMessage != string.Empty)
        {
            //rfvRelationship.ErrorMessage = strErrorMessage;
        }

    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlRelation.Text = strRelation;

            string orgRelationText = ddlRelation.SelectedItem.Text;
            string orgRelationValue = ddlRelation.SelectedItem.Value;

            ddlRelation.Items.Clear();

            ddlRelation.Items.Add(new ListItem(orgRelationText, orgRelationValue));
        }
    }

    public string GetRelationship()
    {
        strRelation = ddlRelation.SelectedValue;
        return strRelation;
    }

}
